package com.example.innovate.service.impl;

import com.example.innovate.domain.*;
import com.example.innovate.mapper.AchievementCompetitionDOMapper;
import com.example.innovate.mapper.StarLevelDOMapper;
import com.example.innovate.mapper.StarPointDOMapper;
import com.example.innovate.service.AchieveCompetitionService;
import com.example.innovate.service.StarPointService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class StarPointServiceImpl implements StarPointService {
    @Autowired
    StarPointDOMapper starPointDOMapper;
    @Autowired
    AchievementCompetitionDOMapper achievementCompetitionDOMapper;
    @Autowired
    StarLevelDOMapper starLevelDOMapper;


    @Override
    public List<StarPointDO> findByList(Integer pageNumber, Integer pageSize) {
        StarPointDOExample starPointDOExample = new StarPointDOExample();
        PageHelper.startPage(pageNumber,pageSize);
        return starPointDOMapper.selectByExample(starPointDOExample);
    }

    @Override
    public void addPoint() {
        AchievementCompetitionDOExample achievementCompetitionDOExample = new AchievementCompetitionDOExample();
        List<AchievementCompetitionDO> list = achievementCompetitionDOMapper.selectByExample(achievementCompetitionDOExample);
        Set<String> set = new HashSet<>();
        // 获取每个学生名字
        for(AchievementCompetitionDO achievementCompetitionDO:list){
            set.add(achievementCompetitionDO.getComAwardsStudent());
        }
        // 遍历获取该学生所有分数并且加起来更新数据
        for(String a:set){
            AchievementCompetitionDOExample temp = new AchievementCompetitionDOExample();
            temp.createCriteria().andComAwardsStudentEqualTo(a);
            List<AchievementCompetitionDO> tempList = achievementCompetitionDOMapper.selectByExample(temp);
            int point = 0;
            StarPointDO starPointDO = new StarPointDO();
            for(AchievementCompetitionDO achievementCompetitionDO:tempList){
                starPointDO.setSpCreateClass(achievementCompetitionDO.getComClassName());
                String level = achievementCompetitionDO.getComLevel();
                String honor = achievementCompetitionDO.getComHonorLevel();
                StarLevelDOExample starLevelDOExample = new StarLevelDOExample();
                starLevelDOExample.createCriteria().andStarLevelEqualTo(level)
                        .andStarRankEqualTo(honor);
                List<StarLevelDO> starLevelDOS = starLevelDOMapper.selectByExample(starLevelDOExample);
                if(starLevelDOS.size()>0){
                    StarLevelDO starLevelDO = starLevelDOS.get(0);
                    point += Integer.valueOf(starLevelDO.getStarPoint());
                }
            }
            starPointDO.setSpPoint(point+"");
            starPointDO.setSpName(a);
            StarPointDOExample starPointDOExample = new StarPointDOExample();
            starPointDOExample.createCriteria().andSpNameEqualTo(a);
            if(starPointDOMapper.selectByExample(starPointDOExample).size()>0){
                starPointDOMapper.updateByExampleSelective(starPointDO,starPointDOExample);
            }
            else{
                starPointDOMapper.insert(starPointDO);
            }

        }
    }
}
